package in.equilibr.ingredients.service;

import in.equilibr.ingredients.model.Ingredient;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import com.google.gson.Gson;
// json dependency

@Service
public class IngredientService {
	private static final Logger logger = LoggerFactory.getLogger(IngredientService.class);
	
	public ModelAndView buildListView(List<Ingredient> ingredients) {
		// it's a new Object for the function/field "Add new ingredient" 
		Ingredient newIngredient = new Ingredient();
		
		ModelAndView mv = new ModelAndView("ingredient");
		mv.addObject("newIngredient", newIngredient);
		mv.addObject("message", "There are " + ingredients.size() + " ingredients read.");
		mv.addObject("ingredients", ingredients);		
		
		return mv;
	}
	
	public Ingredient findIngredientById(List<Ingredient> ingredients, int id) {
		for (Ingredient currentIngredient : ingredients) {
			if(currentIngredient.getId() == id) {
				return currentIngredient;
			}
		}
		return null;
	}
	
	public String convertJson(List<String> values) {
		Gson gson = new Gson();
		
		logger.info("JSON-Object is " + gson.toJson(values));
		
		return gson.toJson(values);
	}
}